package com.dhcc.DRGsHosp.application.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.inject.Named;

import org.dayatang.querychannel.JdbcTemplateObj;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.transaction.annotation.Transactional;

import com.dhcc.DRGsHosp.application.MrqDictRulesApplication;
import com.dhcc.DRGsHosp.core.domain.MrqDictRules;
import com.dhcc.DRGsHosp.infra.util.MonitorResultUtil;

/**
 *
 * @Description:    质控规则字典application层
 * @param:
 * @return:
 * @auther: chenhanlin
 * @date: 2018/7/26 16:35
 */
@Named("drgsHospMrqDictRulesApplicationImpl20181212")
@Transactional
public class MrqDictRulesApplicationImpl implements MrqDictRulesApplication {

	@Autowired
	private JdbcTemplateObj jdbcTemplateObj;
    @Autowired
    private NamedParameterJdbcTemplate jdbcTempSeverice;

    /**
     * 功能描述: 根据主键ruleCode查询数据
     * @param ruleCode
	 * @auther: qiaoyilong
	 * @date: 2018/9/13 10:27
     */
    @Override
    public MrqDictRules getMrqDictRules(String ruleCode){
    	return MrqDictRules.get(MrqDictRules.class, ruleCode);
    }
	/**
	 *
	 * 功能描述: 查询所有的质控规则
	 *
	 * @param:
	 * @return:
	 * @auther: chenhanlin
	 * @date: 2018/7/25 14:27
	 */
	@Override
	public List<MrqDictRules> findAllMrqDictRules(String hospName) {
		StringBuffer sqlb = new StringBuffer() ;
        sqlb.append( " SELECT rule.RULE_CODE \"ruleCode\", rule.DETAIL_CODE \"detailCode\",rule.WEIGHT_CODE \"weightCode\",  \n ") ;
        sqlb.append( " rule.SCORE_CODE \"scoreCode\",rule.RULE_NAME \"ruleName\",rule.RULE_INTRODUCTION \"ruleIntroduction\",rule.COMPLETE_FLAG \"completeFlag\",rule.RULE_SCORE \"ruleScore\" \n ") ;
        sqlb.append( " FROM  drgs.MRQ_DICT_DETAIL detail left join drgs.MRQ_DICT_RULES rule ON detail.DETAIL_CODE = rule.DETAIL_CODE \n ") ;
        sqlb.append( " WHERE detail.VALID_FLAG = '1' and rule.VALID_FLAG = '1' \n ") ;
        sqlb.append( " and detail.score_code in ( select distinct s.score_code from drgs.MRQ_DICT_SCORE s where instr('"+hospName+"',s.hosp_name)>0 ) \n ") ;
		List<MrqDictRules> mrqDictRulesList = jdbcTemplateObj.query(sqlb.toString(),new Object[]{}, new BeanPropertyRowMapper<MrqDictRules>(MrqDictRules.class)) ;
		return mrqDictRulesList;
	}

	/**
	 *  功能描述：根据主键值ruleCode修改字段field的值
	 *  @Author QiaoYiLong
	 *  @param ruleCode 主键
	 *  @param field  字段名称
	 *  @param value  字段值
	 *  @return sql执行成功修改的数据条数
	 *  @Date:2018年8月21日下午2:37:30
	 */
	@Override
	public int updateField(String ruleCode,String field,String value){
		String sql = "UPDATE DRGS.MRQ_DICT_RULES rules SET rules."+field+" = :value WHERE rules.RULE_CODE = :ruleCode ";
		Map<String,Object> paramMap = new HashMap<String,Object>();
		paramMap.put("value", value);
		paramMap.put("ruleCode", ruleCode);
		int count =  jdbcTempSeverice.update(sql, paramMap);
		return count;
	}
	/**
	 *  功能描述：根据detailCode的值查找质控规则
	 *  @Author QiaoYiLong
	 *  @param detailCode  外键
	 *  @return 质控规则列表
	 *  @Date:2018年8月21日下午5:19:36
	 */
	@Override
	public List<Map<String,Object>> getFieldsByDetailCode(String detailCode){
		List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();
		if( null != detailCode && !"".equals(detailCode) ){
			StringBuffer sql = new StringBuffer() ;
			sql.append(" SELECT rules.rule_code \"ruleCode\" ,rules.weight_code \"weightCode\" ,rules.detail_code \"detailCode\" , \n");
			sql.append(" rules.rule_name \"ruleName\", rules.rule_introduction \"ruleIntroduction\" , \n");
			sql.append(" rules.valid_flag  \"validFlag\" FROM  DRGS.MRQ_DICT_RULES rules \n");
			sql.append(" WHERE rules.detail_code = :detailCode ORDER BY rules.rule_code");
			Map<String,Object> paramMap = new HashMap<String,Object>();
			paramMap.put("detailCode", detailCode);
			list = jdbcTempSeverice.queryForList(sql.toString(), paramMap);
		}
		return list;
	}
	
	/**
	 *  功能描述：将detail_code为detailCode的记录中的valid_flag(是否启用字段)全部修改为value
	 *  @Author QiaoYiLong
	 *  @return sql执行成功修改的数据条数
	 *  @Date:2018年9月17日上午11:42:30
	 */
	@Override
	public int updateAll(String detailCode,String value){
		String sql = "UPDATE DRGS.MRQ_DICT_RULES rules SET rules.VALID_FLAG = :value WHERE rules.DETAIL_CODE = :detailCode ";
		Map<String,Object> paramMap = new HashMap<String,Object>();
		paramMap.put("value", value);
		paramMap.put("detailCode", detailCode);
		int count =  jdbcTempSeverice.update(sql, paramMap);
		return count;
	}
}
